$(document).ready(function(){
	$('#add_unit').submit(function() {
		var queryString = $('#add_unit').formSerialize();
		$.post('/domain/unit_add', queryString, function(data){
					if (data.r == 'succ') {
						alert('操作成功');
						window.location.reload();
					} else {
						alert('操作失败' +  "\n>" +  data.msg.join("\n>"));
					}
				}, 'json');
		return false;
	});

	$('#search_button').click(function() {
		var service_id = $('select.service_id').val();
		var user_id = $('select.user_id').val();
		$.get('/domain/unit_search', {'user_id':user_id, 'service_id':service_id}, function(data){
			$('#search_result').html(data);
			$('a.del').click(function() {
					var tr = $(this).parent().parent();
					if (confirm('确定删除?')) {
					$.post('/domain/unit_del', {'unit_id': $(this).attr('unit_id')}, function(data){
							if (data.r == 'succ') {
							tr.fadeOut();
							alert('操作成功');
							//window.location.reload();
							} else {
							alert('操作失败' +  "\n>" +  data.msg.join("\n>"));
							}
							}, 'json');
					}
			});
			function cb(index, jq) {
				$.get('/domain/unit_search', {'user_id':user_id, 'service_id':service_id, 'page':index}, function(data){
					$('#search_result').html(data);
					$('a.del').click(function() {
							var tr = $(this).parent().parent();
							if (confirm('确定删除?')) {
							$.post('/domain/unit_del', {'unit_id': $(this).attr('unit_id')}, function(data){
									if (data.r == 'succ') {
									tr.fadeOut();
									alert('操作成功');
									//window.location.reload();
									} else {
									alert('操作失败' +  "\n>" +  data.msg.join("\n>"));
									}
									}, 'json');
							}
							});
				});
			}
			$('#links').pagination(total, {'callback':cb, 'items_per_page':10, 'num_display_entries':5});
		},'html')
	});

});
